package com.ithei.多线程第二天.送快递2;

public class Demo {
    public static void main(String[] args) {
        new Thread(() -> {
            int number = 0;
            while (true) {
                synchronized (Demo.class) {
                    if (KuaiDi.count == 0) {
                        System.out.println("=================快递投递了========================");
                        break;
                    } else {
                        if (KuaiDi.flag) {
                            try {
                                Demo.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            number++;
                            System.out.println(Thread.currentThread().getName() + "正在投递:    第" + number + "件快递");
                            KuaiDi.flag = true;
                            Demo.class.notifyAll();
                            ;
                        }
                    }
                }
            }

        }, "快递员").start();

        new Thread(()->{
            int number=0;
            while(true){
                synchronized (Demo.class){
                    if(KuaiDi.count==0){
                        System.out.println("=================快递取完了========================");
                        break;
                    }else{
                        if(!KuaiDi.flag){
                            try {
                                Demo.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else{
                            number++;
                            KuaiDi.count--;
                            KuaiDi.flag=false;
                            System.out.println(Thread.currentThread().getName()+"正在取:   第"+number+"件快递");
                            Demo.class.notifyAll();
                        }

                    }
                }
            }


        },"普通用户").start();

    }
}
